package com.sinodata.bsm.cicp.finder.cics;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class CICSTransFinder extends CICSFinder {
    private static final Logger logger = Logger.getLogger(CICSTransFinder.class);

    @Override
    public List<String[]> find(String ip, Map params) {
        String hostname = getHostName(ip);
        List<String[]> reses = new ArrayList<String[]>();
        String cmf = (String) params.get("CMF");
        String fullName = (String) params.get("FULLNAME");
        String shell = "export-tran.sh " + cmf + " outfile all";
        String regex = "\\s*(\\S+)\\s+(\\S+)\\s+(\\S*)";
        Pattern pattern = Pattern.compile(regex);
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0);
            String strLine = null;
            while ((strLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    /*
                     * 每一个String[]表示一个资源数据，String[]数据定义如下：
                     * 第一个元素：资源的内部标示
                     * 第二个元素：资源名称
                     * 第三个元素：资源描述
                     * 第四-第N个参数：返回的属性值
                     */
                    String[] res = new String[5];
                    reses.add(res);
                    res[0] = hostname + "_" + matcher.group(1);
                    res[1] = matcher.group(1);
                    res[2] = matcher.group(1) + " for " + hostname;
                    res[3] = fullName;
                    res[4] = matcher.group(1);
                }
            }
            return reses;
        } catch (IOException e) {
            logger.error("failed to find cicstrans", e);
            e.printStackTrace();
        }
        return null;
    }
}
